/**
 * @filename:ContractController 2021-11-09 01:40:35
 * @project com/lwl/blue  V1.0
 * Copyright(c) 2020 <a href=mailto:lanwenliang@yidianlife.com>蓝文良</a> Co. Ltd.
 * All right reserved.
 */
package com.lwl.blue.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lwl.blue.commont.base.*;
import com.lwl.blue.commont.utils.UserContextUtil;
import com.lwl.blue.entity.Contract;
import com.lwl.blue.entity.ContractDetail;
import com.lwl.blue.entity.Customer;
import com.lwl.blue.service.ContractDetailService;
import com.lwl.blue.service.ContractService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>自定义方法写在这里</p>
 *
 * <p>说明： 合同表API接口层</P>
 *
 * @version: V1.0
 * @author: <a href=mailto:lanwenliang@yidianlife.com>蓝文良</a>
 */
// @Api(tags = "合同表" )
@RestController
@RequestMapping("/contract")
@Slf4j
public class ContractController extends BaseController {
    @Autowired
    ContractService contractService;
    @Resource
    ContractDetailService contractDetailService;

    @PostMapping("/pages")
    public PageResult<Contract> getPages(@RequestBody PageParam<Contract> pageParam) {
        JsonResult<Page<Contract>> pages = contractService.pages(pageParam);
        PageResult<Contract> re = new PageResult<>();
        re.success(pages.getData().getTotal(), pages.getData().getRecords());
        return re;
    }

    @PostMapping("saveOrUpdate")
    public JsonResult saveOrUpdate(@RequestBody @Valid Contract contract) {
        setInsetOrUpdateUser(contract);
        QueryWrapper<Contract> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Contract::getContractNo, contract.getContractNo())
                .eq(Contract::getIsDelete, 0);
        if (null != contract.getId()) {
            wrapper.lambda().notIn(Contract::getId, contract.getId());
        }
        int count = contractService.count(wrapper);
        if (count > 0) {
            throw new BizException(ErrorCode.DATA_EXISTS_ERROR.getCode(), ErrorCode.DATA_EXISTS_ERROR.getMsg() + contract.getContractNo());
        }
        contractService.saveOrUpdate(contract);
        return new JsonResult().success("请求成功");
    }

    @PostMapping("/delete/{id}")
    public JsonResult saveOrUpdate(@PathVariable(value = "id") Integer id) {
        contractService.update(new UpdateWrapper<Contract>().lambda()
                .set(Contract::getIsDelete, Const.IS_DELETE_YES)
                .set(Contract::getUpdateUser, UserContextUtil.getUser().getUserName())
                .eq(Contract::getId, id));

        contractDetailService.update(new UpdateWrapper<ContractDetail>().lambda()
                .set(ContractDetail::getIsDelete, Const.IS_DELETE_YES)
                .set(ContractDetail::getUpdateUser, UserContextUtil.getUser().getUserName())
                .eq(ContractDetail::getContractId, id));
        return new JsonResult().success("请求成功");
    }

    @PostMapping("/select")
    public JsonResult select() {
        List<Contract> reList = contractService.list(new QueryWrapper<Contract>()
                .lambda().eq(Contract::getIsDelete, 0)
                .orderByDesc(Contract::getId));
        return new JsonResult().success(reList);
    }

    @GetMapping("/createCustomerContract/{id}")
    public void createCustomerContract(HttpServletResponse response, @PathVariable(value = "id") Integer id) {

    }

    @GetMapping("/createSupplierContract/{contractId}/{supplierId}")
    public void createSupplierContract(HttpServletResponse response, @PathVariable(value = "contractId") Integer contractId, @PathVariable(value = "supplierId") Integer supplierId) {
        contractService.createSupplierContract(response, contractId, supplierId);
    }

}
